package com.hwz.controller.admin;

import com.hwz.constant.MessageConstant;
import com.hwz.result.Result;
import com.hwz.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/3/15 13:24]
 */
@RestController
@Slf4j
@RequestMapping("/admin/common")
public class CommonController {
    @Autowired
    AliOssUtil aliOssUtil;

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        log.info("上传文件:{}", file.getOriginalFilename());

        // 文件上传的逻辑处理
        try {
            String originalFilename = file.getOriginalFilename();
            String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String fileNewName = UUID.randomUUID().toString() + fileExtension;
            String filePath = aliOssUtil.upload(file.getBytes(),fileNewName);
            log.info("上传成功，文件路径:{}", filePath);
            return Result.success(filePath);
        } catch (IOException e) {
            log.info("上传失败:{}", e.getMessage());
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
